const pathResolver = require('../config/path_resolver');
const pageHelper = require('../../config/path_import.js').pageHelper;
const ProjectBiz = require('../../../comm/biz/project_biz.js');
const projectSetting = require('../../../../../config/projects_config.js');
const FlowBiz = require('../../../biz/flow_biz.js');
const { logEvent } = require('../../../helpers/log_helper.js');
const { getNetworkStatus, listenNetworkStatus } = require('../../../helpers/network_helper.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);
		
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
		
		// 网络检测
		const net = await getNetworkStatus();
		if (!net.isConnected) {
			wx.showToast({ title: '当前无网络，请检查连接', icon: 'none' });
		} else if (net.networkType === '2g') {
			wx.showToast({ title: '当前为2G弱网，体验可能受影响', icon: 'none' });
		}
		listenNetworkStatus((res) => {
			if (!res.isConnected) wx.showToast({ title: '网络已断开', icon: 'none' });
			else if (res.networkType === '2g') wx.showToast({ title: '当前为2G弱网', icon: 'none' });
		});
		logEvent('page_load', { page: 'flow_index' });
		
		this._loadList();
	},

	_loadList: function() {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
		
		this.setData({ 
			list: projectSetting.FLOW_ALL_STEPS 
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
		
		this._loadList();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
		
		this._loadList();
		wx.stopPullDownRefresh();
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
	},

	url: async function (e) {
		// 强制设置登录状态
		FlowBiz.forceSetLoginState(this, 'flow_index');
		
		pageHelper.url(e, this);
	},

	switchTab: function(e) {
		let tab = e.currentTarget.dataset.tab;
		logEvent('switch_tab', { tab });
		this.setData({
			currentTab: tab
		});
	}
})